/**
@file
	ConnectionImpl.h
@brief

@author
	GengYong
@revision
	2005/11/05 * Created by GengYong.
*/

#ifndef SAF_HEADER_FILE_ConnectionImpl_H
#define SAF_HEADER_FILE_ConnectionImpl_H

#include "../include/KernelExport.h"

namespace SAF
{
	class ConnectionImpl:public IConnection
	{
		friend class InternalServiceModule;
	public:
		enum { MaxState = 256 };
	public:	
		ConnectionImpl(IMessageRouter *, unsigned long nodeid, unsigned long stateid, void * cookie);
		virtual ~ConnectionImpl();
	public:	
		virtual bool	EnterState(const char * statename, void *);
		virtual int		LeaveState();
	public:
		virtual void *	Store(const char *, const void *, unsigned long size) const;
		virtual void *	Restore(const char *, unsigned long * psize) const;
	private:
		typedef struct StateCookie
		{
			int			stateId;
			void *		cookie;
		} StateCookie;
	private:
		class StoreTree
		{		
		public:
			StoreTree(const char * name, unsigned long size);
			~StoreTree();
		public:
			const char  *	name;
			unsigned long	size;
			void *			data;
			StoreTree	*	lleaf;
			StoreTree	*	rleaf;
		private:
			StoreTree(){;}
			StoreTree(const StoreTree &){;}
			StoreTree& operator=(const StoreTree &) { return *this; }
		};
	private:	
		mutable StoreTree *	m_storeRoot;
		StateCookie			m_stateStack[MaxState];
		int					m_stateIndex;
	};
}

#endif	//#ifndef SAF_HEADER_FILE_ConnectionImpl_H
